<?php
namespace app\admin\controller;
use catetree\Catetree;
use think\Db;
use think\Cache;
class Index extends Common
{
    public function index()
    {
    	//获取左侧菜单
    	$this->_getMenu();
        return view();
    }
    //控制台数据
    public function welcome()
    {
        header('Location:/admin.php/index/welcome_');
        exit();
        $adminId = session('uid','','admin');
        $ltData = db('admin')->field('month_company_rank,month_depart_rank')->where('id',$adminId)->find();
        $ltData['name'] = session('uname','','admin');
        $ltData['month_qd_count'] = db('client')->where('status',4)->where('xaid',$adminId)->where('qiandan_time','>=',strtotime(date('Y-m-01')) )->where('qiandan_time','<=',strtotime(date('Y-m-t')) )->count();
        $ltData['month_performance_sum'] = db('performance')->where('end_date','>=',strtotime(date('Y-m-01')))->where('end_date','<=',strtotime(date('Y-m-t')))->where('status','>=',1)->where('admin_id',$adminId)->sum('salesman_final_performance');
        
        $lbData = db('admin')->field('id,name,month_performance,month_company_rank,month_depart_rank,department_id')->where('month_company_rank','>=',1)->where('group_id',2)->order('month_company_rank ASC')->limit(10)->select();
        foreach($lbData as $k=>$v)
        {
            $lbData[$k]['month_performance'] = $this->performanceFormat($v['month_performance']);
            $lbData[$k]['depart_name'] = $this->departFormat($v['department_id']);
        }
        
        $rcData = ['name'=>'龚美华','id'=>22];
        $rtData = ['name'=>'星权科技','id'=>4];
        
        $this->assign([
            'ltData'=>$ltData,
            'lbData'=>$lbData,
            'rcData'=>$rcData,
            'rtData'=>$rtData,
        ]);
        return view('welcome_');
    }
    
    public function welcome_()
    {
        $adminId = session('uid','','admin');
        if(!Cache::get('10day_updated'))
        {
            $adminList = db('admin')->where('group_id','IN','5,2')->where('status',1)->select();
            $time1 = strtotime('2022-10-01 00:00:00');
            $time2 = strtotime('2023-01-16 12:00:00');
            foreach ($adminList as $k=>$v)
            {
                $performance = db('performance')->where('end_date','>=', $time1)->where('end_date','<=',$time2 )->where('status','>=',1)->where('admin_id',$v['id'])->sum('salesman_final_performance');
                if(db('topic_10day_log')->where('admin_id',$v['id'])->find() )
                {
                    db('topic_10day_log')->where('admin_id',$v['id'])->update(['performance'=> $performance,'name'=>$v['name'] ] );
                }
                else
                {
                    db('topic_10day_log')->insert(['admin_id'=>$v['id'] , 'performance'=> $performance,'name'=>$v['name'] ] );
                }
            }

            $list = Db::connect("live_db_config")->table('ims_wzbagent_lives')->where('depart_id',0)->where('admin_id','<>',204)->select();
    
            foreach ($list as $k=>$v)
            {
                $data =  Db::connect("live_db_config")->table('ims_wzbagent_managers')->where('id',$v['admin_id'])->find();
                Db::connect("live_db_config")->table('ims_wzbagent_lives')->where('id',$v['id'])->update(['depart_id'=>$data['depart_id'] ]);
            }
            
            Cache::set('10day_updated',1,3600*4);
        }

        $completeData = db('topic_10day_log')->order('performance DESC')->limit(8)->select();
        foreach($completeData as $k=>$v)
        {
            $completeData[$k]['performance'] = $v['performance'] > 100000?100000:$v['performance'];
            $completeData[$k]['progress'] = ceil($completeData[$k]['performance']/1000);
        }
        $myData = db('topic_10day_log')->where('admin_id',$adminId)->find();
        $myData['performance'] = $myData['performance'] > 100000?100000:$myData['performance'];
        $myData['progress'] = ceil($myData['performance']/1000);
        
        $this->assign([
            'completeData'=>$completeData,
            'myData'=>$myData,
        ]);
        return view('welcome_');
    }
    
    public function welcome4()
    {

        return view();
    }
    
    private function performanceFormat($performance)
    {
        if($performance<30000)
            return 'B';
        else if($performance < 50000)
            return 'A';
        else if($performance < 80000)
            return 'A+';
        else if($performance < 100000)
            return 'A++';
        else if($performance < 200000)
            return 'S';
        else if($performance < 300000)
            return 'SS';
        else if($performance < 400000)
            return 'SSS';
        else if($performance < 500000)
            return 'SSSS';
        else
            return 'SSSSS';
    }
    private function departFormat($id)
    {
        switch ($id) {
            case 1:
                return '快乐星球';
                break;
            case 2:
                return '奋斗的大鲸鱼';
                break;
            case 3:
                return '勤奋的小雪鲸';
                break;
            case 4:
                return '努力的小海豚';
                break;
            default:
                return '';
                break;
        }
    }
    
    public function welcome3()
    {
        return view();
    }
    
    public function uploadAvatar()
    {
        if(request()->file('file'))
        {
            $image = \think\Image::open(request()->file('file'));
            $image->thumb(500, 500,\think\Image::THUMB_SCALING )->save(AVATARIMG.'admin_'.session('uid','','admin').'.png');
            
            
            /*$file = request()->file('file');
            $info = $file->move(AVATARIMG);
            $data = array();  //定义一个返回的数组
            if($info){
                $reubfo['code']= 1;
                $reubfo['savename'] = str_replace("\\","/",$info->getSaveName());
            }else{
                // 上传失败获取错误信息
                $reubfo['code']= 0;
                $reubfo['err'] = $file->getError();
            } */
            return json_encode(['code'=>1]);
        }
        else
        {
            return view();
        }
    }
    
    public function test()
    {
        return view();
    }
    
    //左侧菜单
    private function _getMenu()
    {
        $uid=session('uid','','admin');
        $rules=db('admin')->alias('a')->field('a.group_id,b.rules')->where('a.id','=',$uid)->join('auth_group b','a.group_id = b.id')->find();
//        echo "<pre>";
//        print_r($uid);
//        echo "<br>";
//        print_r($rules);
//        die;

		if($uid == 12)
		    $rules['rules'] .= ',354,355,356';
        $ruleArr=explode(',', $rules['rules']);

    	$menuRes=db('auth_rule')->where(['pid'=>0,'menu'=>1,'status'=>1])->where('id','in',$ruleArr)->order('sort')->select();
    	foreach ($menuRes as $k => $v) {
    		$menuRes[$k]['children']=db('auth_rule')->where(['pid'=>$v['id'],'menu'=>1,'status'=>1])->where('id','in',$ruleArr)->order('sort')->select();
    	}
    	$this->assign([
    		'menuRes'=>$menuRes,
    	]);
    }


}
